//
//  NSDate+SR.m
//  SportRing
//
//  Created by mykoma on 14-6-16.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "NSDate+Ex.h"
#import "DateTools.h"

@implementation NSDate (Ex)

-(BOOL)isSameDay:(NSDate *)date
{
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    NSString * todayString = [dateFormatter stringFromDate:self];
    NSString * otherDateString = [dateFormatter stringFromDate:date];
    return [todayString isEqualToString:otherDateString];
}

- (NSString *)formattedBy_yyyyMM
{
    return [self formattedDateWithFormat:@"yyyy-MM"];
}

- (NSString *)formattedBy_yyyyMMdd
{
    return [self formattedDateWithFormat:@"yyyy-MM-dd"];
}

- (NSString *)formattedBy_yyyyMd_CHN
{
    return [self formattedDateWithFormat:@"yyyy年M月d日"];
}

- (NSString *)formattedBy_MMddhhmm {
    return [self formattedDateWithFormat:@"MM-dd hh:mm"];
}

- (NSString *)formattedBy_Mdhhmm_CHN {
    return [self formattedDateWithFormat:@"M月d日 hh:mm"];
}

- (NSString *)formattedBy_yyyyMMddhhmm
{
    return [self formattedDateWithFormat:@"yyyy年M月d日 hh:mm"];
}

- (NSString *)formattedBy_yyyyMMddhhmmss
{
    return [self formattedDateWithFormat:@"yyyy-MM-dd hh:mm:ss"];
}

- (NSString *)formattedBy_hhmm
{
    return [self formattedDateWithFormat:@"HH:mm"];
}

- (NSString *)formattedBy_hhmmss
{
    return [self formattedDateWithFormat:@"HH:mm:ss"];
}

- (NSString *)formattedBy_M_d {
    return [self formattedDateWithFormat:@"MM-dd"];
}

- (NSString *)continuousTimeSinceNow {
    NSDate *now = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekOfYearCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit;
    NSDate *earliest = [self earlierDate:now];
    NSDate *latest = (earliest == self) ? now : self;
    NSDateComponents *components = [calendar components:unitFlags fromDate:earliest toDate:latest options:0];
    
    NSInteger year = components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    NSInteger hour = components.hour;
    NSInteger minute = components.minute;

    NSString *yearSting = [NSString stringWithFormat:@"%ld年",year];
    NSString *monthSting = [NSString stringWithFormat:@"%ld月",month];
    NSString *daySting = [NSString stringWithFormat:@"%ld天",day];
    NSString *hourSting = [NSString stringWithFormat:@"%ld小时",hour];
    NSString *minuteSting = [NSString stringWithFormat:@"%ld分",minute];

    NSString *timeString = [NSString stringWithFormat:@"%@%@%@%@%@",yearSting,monthSting,daySting,hourSting,minuteSting];
    return timeString;
}

- (NSInteger)weekday_CHN
{
    NSInteger weekday_en = [self weekday];
    
    if (weekday_en == 1) {
        weekday_en = 0;
    } else {
        weekday_en -= 1;
    }
    return weekday_en;
}

- (NSArray *)weekStartAndweekEndDate_CHN
{
    NSMutableArray *resultArray = [NSMutableArray array];
    NSDate *now = [NSDate date];
    
    NSInteger weekIndex = [self weekday_CHN];
    NSDate *startDate = [self dateByAddingDays:-(weekIndex - 1)];
    
    if (startDate.timeIntervalSince1970 > now.timeIntervalSince1970) {
        [resultArray addObject:now];
    } else {
        [resultArray addObject:startDate];
    }
    
    if ([self isSameDay:now]) {
        [resultArray addObject:now];
    } else {
        NSDate *endDate = [self dateByAddingDays:(7-weekIndex)];
        if (endDate.timeIntervalSince1970 > now.timeIntervalSince1970) {
            [resultArray addObject:now];
        } else {
            [resultArray addObject:endDate];
        }
    }
    return [NSArray arrayWithArray:resultArray];
}

- (NSArray *)weekStartAndweekEndDate
{
    NSMutableArray *resultArray = [NSMutableArray array];
    NSDate *now = [NSDate date];
    
//    NSInteger weekIndex = [self weekday_CHN];
    NSInteger weekIndex = [self weekday];
    NSDate *startDate = [self dateByAddingDays:-(weekIndex - 1)];
    
    if (startDate.timeIntervalSince1970 > now.timeIntervalSince1970) {
        [resultArray addObject:now];
    } else {
        [resultArray addObject:startDate];
    }
    
    if ([self isSameDay:now]) {
        [resultArray addObject:now];
    } else {
        NSDate *endDate = [self dateByAddingDays:(7-weekIndex)];
        if (endDate.timeIntervalSince1970 > now.timeIntervalSince1970) {
            [resultArray addObject:now];
        } else {
            [resultArray addObject:endDate];
        }
    }
    return [NSArray arrayWithArray:resultArray];
}

- (NSArray *)monthStartAndmonthEndDate
{
    NSMutableArray *resultArray = [NSMutableArray array];
    NSDate *now = [NSDate date];
    NSInteger allDays = [self daysInMonth];
    NSInteger dayIndex = [self day];
    
    NSDate *startDate = [self dateByAddingDays:-(dayIndex - 1)];
    if (startDate.timeIntervalSince1970 > now.timeIntervalSince1970) {
        [resultArray addObject:now];
    } else {
        [resultArray addObject:startDate];
    }
    
    if ([self isSameDay:now]) {
        [resultArray addObject:now];
    } else {
        NSDate *endDate = [self dateByAddingDays:(allDays-dayIndex)];
        if (endDate.timeIntervalSince1970 > now.timeIntervalSince1970) {
            [resultArray addObject:now];
        } else {
            [resultArray addObject:endDate];
        }
    }
    return [NSArray arrayWithArray:resultArray];
}

-(NSInteger)daysAgo {
    return [self daysAgo];
}

- (NSDate *)dateBySubtractingDays:(NSInteger)days {
    return [self dateBySubtractingDays:days];
}

- (BOOL)isToday {
    return [self isToday];
}

- (BOOL)isTomorrow {
    return [self isTomorrow];
}

-(BOOL)isYesterday {
    return [self isYesterday];
}

@end
